package com.chatbot.controller;

import com.chatbot.dto.CharacterDTO;
import com.chatbot.dto.CreateCharacterRequest;
import com.chatbot.security.JwtTokenProvider;
import com.chatbot.service.CharacterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/characters")
public class CharacterController {

    @Autowired
    private CharacterService characterService;

    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    // 创建新角色
    @PostMapping
    public ResponseEntity<CharacterDTO> createCharacter(
            Authentication authentication,
            @RequestBody CreateCharacterRequest request) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        CharacterDTO character = characterService.createCharacter(userId, request);
        return ResponseEntity.ok(character);
    }

    // 获取用户的所有角色
    @GetMapping
    public ResponseEntity<List<CharacterDTO>> getUserCharacters(Authentication authentication) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        List<CharacterDTO> characters = characterService.getUserCharacters(userId);
        return ResponseEntity.ok(characters);
    }

    // 获取角色详情
    @GetMapping("/{characterId}")
    public ResponseEntity<CharacterDTO> getCharacter(
            Authentication authentication,
            @PathVariable Long characterId) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        CharacterDTO character = characterService.getCharacter(userId, characterId);
        return ResponseEntity.ok(character);
    }

    // 更新角色信息
    @PutMapping("/{characterId}")
    public ResponseEntity<CharacterDTO> updateCharacter(
            Authentication authentication,
            @PathVariable Long characterId,
            @RequestBody CreateCharacterRequest request) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        CharacterDTO character = characterService.updateCharacter(userId, characterId, request);
        return ResponseEntity.ok(character);
    }

    // 删除角色
    @DeleteMapping("/{characterId}")
    public ResponseEntity<Void> deleteCharacter(
            Authentication authentication,
            @PathVariable Long characterId) {
        Long userId = jwtTokenProvider.getUserIdFromAuthentication(authentication);
        characterService.deleteCharacter(userId, characterId);
        return ResponseEntity.ok().build();
    }
} 